﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "ProfileUsedModelLineParametersCustomizer.h"
#include "DbObjectUtils.h"
#include "IElement.h"
#include "IElementStatus.h"
#include "IExternalData.h"
#include "IExternalDatas.h"
#include "IGenericElement.h"
#include "IModelLine.h"
#include "IParameter.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

//////////////////////////////////////////////////////////////////////////
// ProfileUsedModelLine的CustomizableParameter定制行为
DBOBJECT_DATA_DEFINE(ProfileUsedModelLineParametersCustomizer)
{
    SetOwnerElement(nullptr);
}
ProfileUsedModelLineParametersCustomizer::ProfileUsedModelLineParametersCustomizer(const IElement* pElement)
    : m_pOwnerElement(const_cast<IElement*>(pElement))
{
}

ProfileUsedModelLineParametersCustomizer::~ProfileUsedModelLineParametersCustomizer()
{
}

OwnerPtr<IParameter> ProfileUsedModelLineParametersCustomizer::GetNativeParameter(int paramDefId) const
{
    const IGenericElement* pGenericElement = dynamic_cast<const IGenericElement*>(GetOwnerElement());
    DBG_WARN_AND_RETURN_UNLESS(pGenericElement, nullptr,L"pGenericElement为空",L"GDMPLab",L"2024-03-30");
    const IModelLine* pModelLine = dynamic_cast<const IModelLine*>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_UNLESS(pModelLine, nullptr, L"pModelLine为空",L"GDMPLab",L"2024-03-30");

    const auto& externalDatas = pModelLine->GetExternalDatas()->GetAllExternalDatas();
    FOR_EACH(externalData, externalDatas)
    {
        OwnerPtr<IParameter> param = externalData->GetNativeParameter(paramDefId);
        if (IParameter::IsValidParameter(param))
        {
            return param;
        }
    }
    return nullptr;
}

bool ProfileUsedModelLineParametersCustomizer::SetNativeParameter(const IParameter *param, std::wstring* errorMsg)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(param, L"param is null.",L"GDMPLab",L"2024-03-30");

    if (errorMsg)
    {
        errorMsg->clear();
    }

    IGenericElement* pGenericElement = dynamic_cast<IGenericElement*>(GetOwnerElement());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pGenericElement, L"pGenericElement为空",L"GDMPLab",L"2024-03-30");

    IModelLine* pModelLine = dynamic_cast<IModelLine*>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelLine, L"pModelLine为空",L"GDMPLab",L"2024-03-30");

    auto& externalDatas = pModelLine->GetExternalDatas()->GetAllExternalDatas();
    FOR_EACH(externalData, externalDatas)
    {
        if (externalData->SetNativeParameter(param, errorMsg))
        {
            return true;
        }
    }
    return false;
}

void ProfileUsedModelLineParametersCustomizer::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pParamDefIds != nullptr, L"pParamDefUids为空",L"GDMPLab",L"2024-03-30");
    const IGenericElement* pGenericElement = dynamic_cast<const IGenericElement*>(GetOwnerElement());
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGenericElement, L"pGenericElement为空",L"GDMPLab",L"2024-03-30");
    const IModelLine* pModelLine = dynamic_cast<const IModelLine*>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_VOID_UNLESS(pModelLine, L"pModelLine为空",L"GDMPLab",L"2024-03-30");

    const auto& externalDatas = pModelLine->GetExternalDatas()->GetAllExternalDatas();
    FOR_EACH(externalData, externalDatas)
    {
        externalData->ReportParameterDefinitions(pParamDefIds);
    }
}

bool ProfileUsedModelLineParametersCustomizer::IsParameterModifiable(int paramDefId) const
{
    const IGenericElement* pGenericElement = dynamic_cast<const IGenericElement*>(GetOwnerElement());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pGenericElement, L"pGenericElement为空",L"GDMPLab",L"2024-03-30");
    const IModelLine* pModelLine = dynamic_cast<const IModelLine*>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pModelLine, L"pModelLine为空",L"GDMPLab",L"2024-03-30");

    const auto& externalDatas = pModelLine->GetExternalDatas()->GetAllExternalDatas();
    FOR_EACH(externalData, externalDatas)
    {
        if (!externalData->IsParameterModifiable(paramDefId))
        {
            return false;
        }
    }
    return pGenericElement->GetStatus()->IsModifiable();
}
